package com.baijiaoxi.utils.qcloud;

import com.baijiaoxi.common.model.cache.CloudConfigInfo;
import com.baijiaoxi.common.utils.jdk.FileUtil;
import com.baijiaoxi.common.utils.jdk.Strings;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class FileDownloadHelper {
    private String innerEndpointArea;
    private static final Pattern domainPattern = Pattern.compile("https?://([^/]+)");

    private String accessKey;
    private String secretKey;

    public FileDownloadHelper(String accessKey, String secretKey, String innerEndpointArea) {
        this.innerEndpointArea = innerEndpointArea;//shanghai、hangzhou类似的
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public static String createFilePath(String fileCatalog, String fileName) {
        File storageFile = new File(fileCatalog);
        if (!storageFile.exists())
            storageFile.mkdirs();
        String localPath = storageFile.getPath() + File.separator + fileName;
        return localPath;
    }

    public String download(String url, String fileKey, String fileCatalog) {
        String fileFullPath = createFilePath(fileCatalog, fileKey);
        return downloadToPath(url, fileKey, fileFullPath);
    }

    public String downloadAndRename(String url, String fileKey, String fileCatalog, String filename) {
        String fileFullPath = createFilePath(fileCatalog, filename);
        return downloadToPath(url, fileKey, fileFullPath);
    }

    public String downloadToPath(String url, String fileKey, String fileFullPath) {
        if (new File(fileFullPath).exists())
            return fileFullPath;
        boolean handled = false;

        if (Strings.isNotBlank(innerEndpointArea)) {
            Matcher matcher = domainPattern.matcher(url);
            if (matcher.find()) {
                String domain = matcher.group(1);
                String bucketNameRegex = "([^\\.]+)\\.oss-cn-";
                Pattern bucketNamePattern = Pattern.compile(bucketNameRegex);
                Matcher bucketNameMatcher = bucketNamePattern.matcher(domain);
                if (bucketNameMatcher.find()) {
                    String bucketName = bucketNameMatcher.group(1);
                    log.info("提取的Bucket名称: {}", bucketName); // 输出: 提取的Bucket名称: baijiaoxi-static
                    if (domain.equals(String.format("%s.oss-cn-%s.aliyuncs.com", bucketName, innerEndpointArea))) {
                        //https://baijiaoxi-static.oss-cn-shanghai.aliyuncs.com/2021.12.30/ddce970c70c52dbed29864c6976cdf3.jpg
                        OSHelper osHelper = OSHelperFactory.create(new CloudConfigInfo() {
                            @Override
                            public String getAccessKey() {
                                return accessKey;
                            }

                            @Override
                            public String getSecretKey() {
                                return secretKey;
                            }

                            @Override
                            public String getBucketName() {
                                return bucketName;
                            }

                            @Override
                            public String getRegionName() {
                                return String.format("oss-cn-%s.aliyuncs.com", innerEndpointArea);
                            }

                            @Override
                            public String getProvider() {
                                return "oss";
                            }
                        }, false);
                        osHelper.download(fileKey, fileFullPath);
                        log.info("文件下载成功: {}", fileFullPath);
                        handled = true;
                    }
                }
            }
        }
        if (!handled)
            FileUtil.download(url, fileFullPath);
        return fileFullPath;
    }


}
